import wx
from pyDemin import *



class DeminWindow(wx.Panel):
	def __init__(self, parent, ID):
		wx.Panel.__init__(self,parent,ID)
		self.SetSize(parent.GetClientSize())
		self.init_buffer()
		self.Bind(wx.EVT_PAINT,self.OnPaint)
		self.Bind(wx.EVT_LEFT_DOWN,self.OnLeft)
		self.Bind(wx.EVT_RIGHT_DOWN,self.OnRight)
		self.gw=0
		
	def OnLeft(self,evt):
		x,y = evt.GetPosition()
		if self.gw==0:
			k=0
			if Demi.deco[int(x/30),int(y/30)]!=2:
				a=Demi.devoil(int(x/30),int(y/30))
				Demi.recursion(int(x/30),int(y/30))
				if a!=-1:	
					self.init_buffer()
				else:
					self.init_buffer2()
					self.gw=1
			for i in range(Demi.l):
				for j in range(Demi.L):
					if Demi.deco[i,j]==1:
						k+=1
			if k==(Demi.l*Demi.L-Demi.nm):
				self.gw=1
				self.init_buffer3()

					
	def OnRight(self,evt):
		x,y=evt.GetPosition()
		if self.gw==0:
			if Demi.deco[int(x/30),int(y/30)]==0:
				Demi.deco[int(x/30),int(y/30)]=2
			
			elif Demi.deco[int(x/30),int(y/30)]==2:
				Demi.deco[int(x/30),int(y/30)]=0
			self.init_buffer()	




	def OnPaint(self,evt):
		dc = wx.BufferedPaintDC(self,self.buffer)
	def init_buffer(self):
		w,h = self.GetClientSize()
		self.buffer = wx.EmptyBitmap(w,h)
		dc = wx.BufferedDC(wx.ClientDC(self),self.buffer)
		self.draw(dc)
	def init_buffer2(self):
		w,h = self.GetClientSize()
		self.buffer = wx.EmptyBitmap(w,h)
		dc = wx.BufferedDC(wx.ClientDC(self),self.buffer)
		self.draw2(dc)
	def init_buffer3(self):
		w,h = self.GetClientSize()
		self.buffer = wx.EmptyBitmap(w,h)
		dc = wx.BufferedDC(wx.ClientDC(self),self.buffer)
		self.draw3(dc)	
	def draw(self,dc):
		dc.Clear()
		dc.SetBrush(wx.Brush("red"))
		dc.SetPen(wx.Pen("black",1))
		k=0
		for i in range(Demi.l):
			for j in range(Demi.L):
				if Demi.deco[i,j]==0:
					dc.SetBrush(wx.Brush("grey"))
					dc.DrawRectangle(i*600/Demi.l,j*600/Demi.L,600/Demi.l,600/Demi.L)
				elif Demi.deco[i,j]==1:
					dc.DrawText("%d"%Demi.inf[i,j],i*600/Demi.l+11,j*600/Demi.L+7)
				else:
					dc.SetBrush(wx.Brush("grey"))
					dc.DrawRectangle(i*600/Demi.l,j*600/Demi.L,600/Demi.l,600/Demi.L)
					dc.SetBrush(wx.Brush("red"))
					dc.DrawLine(i*600/Demi.l+10,j*600/Demi.L+20,i*600/Demi.l+10,j*600/Demi.L+5)
					dc.DrawRectangle(i*600/Demi.l+10,j*600/Demi.L+5,10,7)
					dc.DrawLine(i*600/Demi.l+8,j*600/Demi.L+20,i*600/Demi.l+13,j*600/Demi.L+20)
					k+=1
		dc.DrawText("Nombre de mines restantes : %d"%(Demi.nm-k),20,620)
		a=wx.Button(self,-1,"new",(550,600),(50,25))
		self.Bind(wx.EVT_BUTTON,self.new,a)
		b=wx.Button(self,-1,"quit",(550,625),(50,25))
		self.Bind(wx.EVT_BUTTON,self.fermerfenetre,b)
	def draw2(self,dc):
		dc.Clear()
		dc.SetBrush(wx.Brush("black"))
		dc.SetPen(wx.Pen("black",1))
		for i in range(Demi.l):
			for j in range(Demi.L):
				if Demi.inf[i,j]!=-1:
					dc.DrawText("%d"%Demi.inf[i,j],i*600/Demi.l+11,j*600/Demi.L+7)
				else:
					dc.DrawCircle(i*600/Demi.l+15,j*600/Demi.L+15,15)	
		dc.SetBrush(wx.Brush("yellow"))
		dc.SetPen(wx.Pen("black",3))
		dc.DrawCircle(300,300,200)
		dc.SetBrush(wx.Brush("blue"))
		dc.DrawLine(220,230,220,270)
		dc.DrawLine(200,250,240,250)
		dc.DrawLine(360,250,400,250)
		dc.DrawLine(380,230,380,270)
		dc.SetBrush(wx.Brush("red"))
		dc.DrawLine(150,400,450,400)
		a=wx.Button(self,-1,"new",(550,600),(50,25))
		self.Bind(wx.EVT_BUTTON,self.new,a)
		b=wx.Button(self,-1,"quit",(550,625),(50,25))
		self.Bind(wx.EVT_BUTTON,self.fermerfenetre,b)
	def draw3(self,dc):
		dc.Clear()
		dc.SetBrush(wx.Brush("yellow"))
		dc.SetPen(wx.Pen("black",3))
		dc.DrawCircle(300,300,200)
		dc.SetBrush(wx.Brush("blue"))
		dc.DrawCircle(220,250,20)
		dc.DrawCircle(380,250,20)
		dc.SetBrush(wx.Brush("yellow"))
		dc.DrawArc(150,300,450,300,300,300)
		dc.SetPen(wx.Pen("yellow",3))
		dc.DrawLine(150,300,450,300)
		a=wx.Button(self,-1,"new",(550,600),(50,25))
		self.Bind(wx.EVT_BUTTON,self.new,a)
		b=wx.Button(self,-1,"quit",(550,625),(50,25))
		self.Bind(wx.EVT_BUTTON,self.fermerfenetre,b)
	def new(self,event):
		Demi.reset()
		Demi.comptemine()
		self.gw=0
		self.init_buffer()
		event.GetEventObject().Destroy()
	def fermerfenetre(self,event):
		self.GetParent().Destroy()		
class GameFrame(wx.Frame):
	def __init__( self, parent, ID, title, pos=wx.DefaultPosition,size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE):
		wx.Frame.__init__(self, parent, ID, title, pos, size, style)
		
		self.SetAutoLayout(True)
		self.GameWindow = DeminWindow(self,-1)
		self.GameWindow.init_buffer()

		
class GameApp(wx.App):
	def OnInit(self):
			win = GameFrame(None, -1, "This is a wx.Demineur", size=(600, 650),style = wx.DEFAULT_FRAME_STYLE)
			win.Show(True)
			self.SetTopWindow(win)
			return True
#app = GameApp()
#app.MainLoop()

